package com.example.haojiutao.handler;


import com.example.haojiutao.common.Result;
import com.example.haojiutao.exception.HaoJiuTaoException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 异常捕获类
 * @Author: wang_meiying
 * @CreateTime: 2024/1/29 20:25
 */
@Slf4j
@ControllerAdvice(annotations = {RestController.class})
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception e){
        log.error("系统异常，原因{}",e.getMessage());
        e.printStackTrace();
        return Result.faile("系统异常，请稍后重试");

    }
    @ResponseBody
    @ExceptionHandler(ArithmeticException.class)
    public Result handlerArithmeticException(ArithmeticException e){
        log.error("系统异常",e.getMessage());
        e.printStackTrace();
        return Result.faile("系统异常，算数错误");
    }


    @ResponseBody
    @ExceptionHandler(HaoJiuTaoException.class)
    public Result handlerMusicException(HaoJiuTaoException e){
        log.error("系统异常",e.getMessage());
        e.printStackTrace();
        return Result.faile(Integer.valueOf(e.getCode()),e.getMessage());
    }
}
